if (typeof(ko.extensions.webos.emulator) == 'undefined')
    ko.extensions.webos.emulator = {};
if (typeof(ko.extensions.webos.device) == 'undefined')
    ko.extensions.webos.device = {};
if (typeof(ko.extensions.webos.deploy) == 'undefined')
    ko.extensions.webos.deploy = {};

ko.extensions.webos.emulator.cmdName = "tcp";
ko.extensions.webos.device.cmdName = "usb";
ko.extensions.webos.emulator.packageInstallLaunch = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isEmulatorConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("EmulatorPackageLaunchInstallButton",
                                                "process",
                                                "");
                ko.extensions.webos.palm.packageApplication({
                    onSuccess:function()
                    {
                        ko.extensions.webos.palm.installApplication({
                            device:ko.extensions.webos.emulator.cmdName,
                            onSuccess:function()
                            {
                                ko.extensions.webos.palm.launchApplication({
                                    device:ko.extensions.webos.emulator.cmdName,
                                    onSuccess:function()
                                    {
                                        ko.extensions.webos.deploy.view("EmulatorPackageLaunchInstallButton",
                                                                        "done",
                                                                        "");
                                    },
                                    onFailure:function()
                                    {
                                        ko.extensions.webos.deploy.view("EmulatorPackageLaunchInstallButton",
                                                                        "error",
                                                                        "Launch could not be completed");
                                    }
                                });
                            },
                            onFailure:function()
                            {
                                ko.extensions.webos.deploy.view("EmulatorPackageLaunchInstallButton",
                                                                "error",
                                                                "Install could not be completed");
                            }
                        });
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("EmulatorPackageLaunchInstallButton",
                                                          "error",
                                                          "Package could not be completed");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("EmulatorPackageLaunchInstallButton",
                                                  "error",
                                                  "Could not connect to Emulator, try restarting the emulator");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.emulator.packageInstallInspect = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isEmulatorConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("EmulatorInspectButton",
                                                  "process",
                                                  "");
                ko.extensions.webos.palm.packageApplication({
                    onSuccess:function()
                    {
                        ko.extensions.webos.palm.installApplication({
                            device:ko.extensions.webos.emulator.cmdName,
                            onSuccess:function()
                            {
                                ko.extensions.webos.palm.inspectApplication({
                                    device:ko.extensions.webos.emulator.cmdName,
                                    onSuccess:function()
                                    {
                                        ko.extensions.webos.deploy.view("EmulatorInspectButton",
                                                                          "done",
                                                                          "");
                                    },
                                    onFailure:function()
                                    {
                                        ko.extensions.webos.deploy.view("EmulatorInspectButton",
                                                                          "error",
                                                                          "Launch could not be completed");
                                    }
                                });
                            },
                            onFailure:function()
                            {
                                ko.extensions.webos.deploy.view("EmulatorInspectButton",
                                                                  "error",
                                                                  "Install to emulator could not be completed");
                            }
                        });
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("EmulatorInspectButton",
                                                        "error",
                                                        "Packaging to ipk could not be completed");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("EmulatorInspectButton",
                                                  "error",
                                                  "Could not connect to Emulator, try restarting the emulator");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.emulator.remove = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isEmulatorConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("EmulatorRemoveButton",
                                                "process",
                                                "");
                ko.extensions.webos.palm.removeApplication({
                    device:ko.extensions.webos.emulator.cmdName,
                    onSuccess:function()
                    {
                        ko.extensions.webos.deploy.view("EmulatorRemoveButton",
                                                        "done",
                                                        "");
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("EmulatorRemoveButton",
                                                        "error",
                                                        "Removing the application from the emulator could not be completed.");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("EmulatorRemoveButton",
                                                "error",
                                                "Could not connect to emulator, try restarting the emulator");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.emulator.launch = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isEmulatorConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("EmulatorLaunchButton",
                                                  "process",
                                                  "");
                ko.extensions.webos.palm.launchApplication({
                    device:ko.extensions.webos.emulator.cmdName,
                    onSuccess:function()
                    {
                        ko.extensions.webos.deploy.view("EmulatorLaunchButton",
                                                        "done",
                                                        "");
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("EmulatorLaunchButton",
                                                        "error",
                                                        "Launching the application in the emulator could not be completed");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("EmulatorLaunchButton",
                                                "error",
                                                "Could not connect to emulator, try restarting the emulator");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.emulator.close = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isEmulatorConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("EmulatorCloseButton",
                                                "process",
                                                "");
                ko.extensions.webos.palm.closeApplication({
                    device:ko.extensions.webos.emulator.cmdName,
                    onSuccess:function()
                    {
                        ko.extensions.webos.deploy.view("EmulatorCloseButton",
                                                          "done",
                                                          "");
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("EmulatorCloseButton",
                                                          "error",
                                                          "Closing application in emulator could not be completed");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("EmulatorCloseButton",
                                                  "error",
                                                  "Could not connect to emulator, try restarting the emulator");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.device.packageInstallLaunch = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isDeviceConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("DevicePackageInstallLaunchButton",
                                                "process",
                                                "");
                ko.extensions.webos.palm.packageApplication({
                    onSuccess:function()
                    {
                        ko.extensions.webos.palm.installApplication({
                            device:ko.extensions.webos.device.cmdName,
                            onSuccess:function()
                            {
                                ko.extensions.webos.palm.launchApplication({
                                    device:ko.extensions.webos.device.cmdName,
                                    onSuccess:function()
                                    {
                                        ko.extensions.webos.deploy.view("DevicePackageInstallLaunchButton",
                                                                        "done",
                                                                        "");
                                    },
                                    onFailure:function()
                                    {
                                        ko.extensions.webos.deploy.view("DevicePackageInstallLaunchButton",
                                                                        "error",
                                                                        "Launching application to the device could not be completed");
                                    }
                                });
                            },
                            onFailure:function()
                            {
                                ko.extensions.webos.deploy.view("DevicePackageInstallLaunchButton",
                                                                "error",
                                                                "Installing application to the device could not be completed");
                            }
                        });
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("DevicePackageInstallLaunchButton",
                                                        "error",
                                                        "Packaging application to the device could not be completed");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("DevicePackageInstallLaunchButton",
                                                "error",
                                                "Could not connect to device, make sure Developer Mode is on and the device is connected");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.device.remove = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isDeviceConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("DeviceRemoveButton",
                                                "process",
                                                "");
                ko.extensions.webos.palm.removeApplication({
                    device:ko.extensions.webos.device.cmdName,
                    onSuccess:function()
                    {
                        ko.extensions.webos.deploy.view("DeviceRemoveButton",
                                                        "done",
                                                        "");
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("DeviceRemoveButton",
                                                        "error",
                                                        "Removing application from the device could not be completed");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("DeviceRemoveButton",
                                                "error",
                                                "Could not connect to device, make sure Developer Mode is on and the device is connected");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.device.launch = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isDeviceConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("DeviceLaunchButton",
                                                "process",
                                                "");
                ko.extensions.webos.palm.launchApplication({
                    device:ko.extensions.webos.device.cmdName,
                    onSuccess:function()
                    {
                        ko.extensions.webos.deploy.view("DeviceLaunchButton",
                                                        "done",
                                                        "");
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("DeviceLaunchButton",
                                                        "error",
                                                        "Launching application to the device could not be completed");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("DeviceLaunchButton",
                                                "error",
                                                "Could not connect to device, make sure Developer Mode is on and the device is connected");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.device.close = function()
{
    if(ko.extensions.webos.project.isWebOS)
    {
        ko.extensions.webos.palm.isDeviceConnected({
            onSuccess:function()
            {
                ko.extensions.webos.deploy.view("DeviceCloseButton",
                                                "process",
                                                "");
                ko.extensions.webos.palm.closeApplication({
                    device:ko.extensions.webos.device.cmdName,
                    onSuccess:function()
                    {
                        ko.extensions.webos.deploy.view("DeviceCloseButton",
                                                        "done",
                                                        "");
                    },
                    onFailure:function()
                    {
                        ko.extensions.webos.deploy.view("DeviceCloseButton",
                                                        "error",
                                                        "Closing application on the device could not be completed");
                    }
                });
            },
            onFailure:function()
            {
                ko.extensions.webos.deploy.view("DeviceCloseButton",
                                                "error",
                                                "Could not connect to device, make sure Developer Mode is on and the device is connected");
            }
        });
    }
    else
    {
        alert('Not a webOS Project');
    }
};

ko.extensions.webos.deploy.view = function(id, mode, msg)
{
    // mode = "process", "done", "error"
    var e = "webos";
    var ele = {
        "EmulatorPackageLaunchInstallButton":{
            normal:"box_add",
            opacity:"box_add_opacity60",
            process:"box_add_refresh",
            error:"box_add_error"},
        "EmulatorInspectButton":{
            normal:"box_magnifier",
            opacity:"box_magnifier_opacity60",
            process:"box_magnifier_refresh",
            error:"box_magnifier_error"},
        "EmulatorRemoveButton":{
            normal:"box_delete",
            opacity:"box_delete_opacity60",
            process:"box_delete_refresh",
            error:"box_delete_error"},
        "EmulatorLaunchButton":{
            normal:"box_go",
            opacity:"box_go_opacity60",
            process:"box_go_refresh",
            error:"box_go_error"},
        "EmulatorCloseButton":{
            normal:"box_cancel",
            opacity:"box_cancel_opacity60",
            process:"box_cancel_refresh",
            error:"box_cancel_error"},
        "DevicePackageInstallLaunchButton":{
            normal:"pre_add",
            opacity:"pre_add_opacity60",
            process:"pre_add_refresh",
            error:"pre_add_error"},
        "DeviceRemoveButton":{
            normal:"pre_delete",
            opacity:"pre_delete_opacity60",
            process:"pre_delete_refresh",
            error:"pre_delete_error"},
        "DeviceLaunchButton":{
            normal:"pre_go",
            opacity:"pre_go_opacity60",
            process:"pre_go_refresh",
            error:"pre_go_error"},
        "DeviceCloseButton":{
            normal:"pre_cancel",
            opacity:"pre_cancel_opacity60",
            process:"pre_cancel_refresh",
            error:"pre_cancel_error"}
    };
    switch(mode)
    {
        case "process":
            for(var i in ele)
            {
                document.getElementById(e + i).
                         image="chrome://webos/skin/" + ele[i].opacity + ".png";
                document.getElementById(e + i).
                         disabled = true;
            }
            document.getElementById(e + id).
                     image="chrome://webos/skin/" + ele[id].process + ".png";
            break;
        case "done":
            for(var i in ele)
            {
                document.getElementById(e + i).
                         image="chrome://webos/skin/" + ele[i].normal + ".png";
                document.getElementById(e + i).
                         disabled = false;
            }
            break;
        case "error":
            for(var i in ele)
            {
                document.getElementById(e + i).
                         image="chrome://webos/skin/" + ele[i].normal + ".png";
                document.getElementById(e + i).
                         disabled = true;
            }
            document.getElementById(e + id).
                     image="chrome://webos/skin/" + ele[id].error + ".png";
            ko.extensions.webos.command.appendConsole(msg);
            setTimeout(function(){
                for(var i in ele)
                {
                    document.getElementById(e + i).
                             image="chrome://webos/skin/" + ele[i].normal + ".png";
                    document.getElementById(e + i).
                             disabled = false;
                }
                document.getElementById(e + id).
                         image="chrome://webos/skin/" + ele[id].normal + ".png";
                document.getElementById(e + id).
                         disabled = false;
                         
            }, 5000);
            break;
    }
};